//
//  CustomModelSwitchView.m
//  AmountBody
//
//  Created by Qiu Jishuai on 2018/1/3.
//  Copyright © 2018年 RCMTM. All rights reserved.
//

#import "CustomModelSwitchView.h"
#import <Masonry/Masonry.h>
#import "Customization.h"

static const CGFloat kButtonW = 92;

@interface CustomModelSwitchView()
@property (nonatomic, strong) NSMutableArray<CustomModelSwitchButton *> *buttons;
@property (nonatomic, strong) CustomModelSwitchButton *lastButton;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) NSMutableArray *lines;
@property (nonatomic, strong) UIImageView *bgView;
@end

@implementation CustomModelSwitchView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
    }
    return self;
}

- (void)setup {
    _bgView = [[UIImageView alloc] init];
    _bgView.contentMode = UIViewContentModeRedraw;
    UIImage *backgruondImage = [UIImage imageNamed:@"custom_model_switch_bg"];
    _bgView.image = [backgruondImage stretchImageWithInsetsTop:10 left:10 bottom:10 right:10];
    [self addSubview:_bgView];
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];

    UIView *container = [[UIView alloc] init];
    container.layer.cornerRadius = 27;
    container.layer.masksToBounds = YES;
    [self addSubview:container];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(3, 3, 3, 3));
    }];
    self.containerView = container;
}

- (void)setIndex:(NSInteger)index {
    _index = index;
    self.buttons[index].enabled = NO;
    self.lastButton = self.buttons[index];
}

- (void)setItems:(NSArray<CustomizationModel *> *)items {
    if ([_items isEqualToArray:items]) {
        return;
    }
    _items = [items copy];
    while (self.buttons.count < self.items.count) {
        CustomModelSwitchButton *button = [[CustomModelSwitchButton alloc] init];

        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self.buttons addObject:button];
        [self.containerView addSubview:button];
    }

    while (self.buttons.count > self.items.count) {
        [self.buttons.lastObject removeFromSuperview];
        [self.buttons removeLastObject];
    }

    [self.buttons enumerateObjectsUsingBlock:^(CustomModelSwitchButton * button, NSUInteger idx, BOOL * stop) {
        button.tag = (NSInteger)idx;
        CustomizationModel *model = items[idx];
        [button setTitle:model.title forState:UIControlStateNormal];
        if (idx == 0) {
            button.inLeft = YES;
        } else {
            button.inLeft = NO;
        }
    }];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    NSInteger count = self.buttons.count;
    [self.buttons enumerateObjectsUsingBlock:^(CustomModelSwitchButton * button, NSUInteger idx, BOOL * stop) {

        [button mas_remakeConstraints:^(MASConstraintMaker *make) {
            layoutViews(^{ //横屏
                UIImage *backgruondImage = [UIImage imageNamed:@"custom_model_switch_bg"];
                _bgView.image = [backgruondImage stretchImageWithInsetsTop:10 left:10 bottom:10 right:10];
                make.top.bottom.equalTo(self.containerView);
                make.width.equalTo(@(kButtonW));
                make.left.equalTo(self.containerView).offset(kButtonW * idx - 2);
                if (idx == count - 1) {
                    make.right.equalTo(self.containerView).offset(-2);
                }
            }, ^{ //竖屏
                UIImage *backgruondImage = [UIImage imageNamed:@"custom_model_switch_bg_p"];
                _bgView.image = [backgruondImage stretchImageWithInsetsTop:10 left:10 bottom:10 right:10];
                make.left.right.equalTo(self.containerView);
                make.height.equalTo(@(kButtonW));
                make.top.equalTo(self.containerView).offset(kButtonW * idx - 2);
                if (idx == count - 1) {
                    make.bottom.equalTo(self.containerView).offset(-2);
                }
            });
        }];
        if (idx == 0) {
            button.inLeft = YES;
        } else {
            button.inLeft = NO;
        }
    }];
}

#pragma mark - Event Response
- (void)buttonClicked: (CustomModelSwitchButton *)sender {
    if (sender.isSelected) {
        return;
    }
    self.lastButton.enabled = YES;
    sender.enabled = NO;
    self.lastButton = sender;

    if (self.switchBlock) {
        self.switchBlock(self.items[sender.tag]);
    }
}

#pragma mark - Getter
- (NSMutableArray *)buttons {
    if (!_buttons) {
        _buttons = [NSMutableArray arrayWithCapacity:3];
    }
    return _buttons;
}

@end


@interface CustomModelSwitchButton()
@property (nonatomic, strong) UIView *line;
@end

@implementation CustomModelSwitchButton

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        UIImageView *line = [[UIImageView alloc] init];
        line.image = [UIImage imageNamed:@"custom_model_switch_line"];
        self.line = line;
        [self addSubview: line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self);
            make.top.bottom.equalTo(self);
        }];
        self.imageView.contentMode = UIViewContentModeScaleAspectFill;
        [self setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
        UIImage *bgImage = [UIImage imageWithColor:[UIColor whiteColor]];
        UIImage *selectedBGImage = [UIImage imageNamed:@"custom_model_switch_btn_selected"];
        [self setBackgroundImage:bgImage forState:UIControlStateNormal];
        [self setBackgroundImage:selectedBGImage forState:UIControlStateHighlighted];
        [self setBackgroundImage:selectedBGImage forState:UIControlStateDisabled];
    }
    return self;
}

- (void)setInLeft:(BOOL)inLeft {
    _inLeft = inLeft;
    self.line.hidden = inLeft;
}

@end
